/*
  cpu_map.h - CPU and pin mapping configuration file
  Part of Grbl

  Copyright (c) 2012-2016 Sungeun K. Jeon for Gnea Research LLC

  Grbl is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Grbl is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
*/

/* The cpu_map.h files serve as a central pin mapping selection file for different
   processor types or alternative pin layouts. This version of Grbl supports only the
   Arduino Mega2560. */

#ifndef cpu_map_h
#define cpu_map_h

#ifdef CPU_MAP_2560_FOURTH_AXIS// (Arduino Mega 2560)

// Serial port interrupt vectors
	#define SERIAL_RX USART0_RX_vect
	#define SERIAL_UDRE USART0_UDRE_vect

// Define step pulse output pins. NOTE: All step bit pins must be on the same port.
	#define STEP_DDR      DDRA
	#define STEP_PORT     PORTA
	#define STEP_PIN      PINA
	#define X_STEP_BIT    0 // MEGA2560 Digital Pin 22
	#define Y_STEP_BIT    2 // MEGA2560 Digital Pin 24
	#define Z_STEP_BIT    4 // MEGA2560 Digital Pin 26
/// +Q
	#define Q_STEP_BIT	  6 // MEGA2560 Digital Pin 28
	#define STEP_MASK ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT)|(1<<Q_STEP_BIT)) // All step bits
///
// Define step direction output pins. NOTE: All direction pins must be on the same port.
	#define DIRECTION_DDR     DDRA
	#define DIRECTION_PORT    PORTA
	#define DIRECTION_PIN     PINC
	#define X_DIRECTION_BIT   1 // MEGA2560 Digital Pin 23
	#define Y_DIRECTION_BIT   3 // MEGA2560 Digital Pin 25
	#define Z_DIRECTION_BIT   5 // MEGA2560 Digital Pin 27
/// +Q
	#define Q_DIRECTION_BIT	  7 // MEGA2560 Digital Pin 29
// All direction bits
	#define DIRECTION_MASK ((1<<X_DIRECTION_BIT)|(1<<Y_DIRECTION_BIT)|(1<<Z_DIRECTION_BIT)|(1<<Q_DIRECTION_BIT))
///
// Define stepper driver enable/disable output pin.
	#define STEPPERS_DISABLE_DDR   DDRC
	#define STEPPERS_DISABLE_PORT  PORTC
	#define STEPPERS_DISABLE_BIT   7 // MEGA2560 Digital Pin 30
	#define STEPPERS_DISABLE_MASK (1<<STEPPERS_DISABLE_BIT)

// Define homing/hard limit switch input pins and limit interrupt vectors.
	// NOTE: All limit bit pins must be on the same port
	#define LIMIT_DDR       DDRB
	#define LIMIT_PORT      PORTB
	#define LIMIT_PIN       PINB
	#define X_LIMIT_BIT     4 // MEGA2560 Digital Pin 10
	#define Y_LIMIT_BIT     5 // MEGA2560 Digital Pin 11
	#define Z_LIMIT_BIT     6 // MEGA2560 Digital Pin 12
/// +Q
	#define Q_LIMIT_BIT     7  // MEGA2560 Digital Pin 13
	#define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT)|(1<<Q_LIMIT_BIT)) // All limit bits
///
	#define LIMIT_INT       PCIE0  // Pin change interrupt enable pin
	#define LIMIT_INT_vect  PCINT0_vect
	#define LIMIT_PCMSK     PCMSK0 // Pin change interrupt register

// Define spindle enable and spindle direction output pins.
	#define SPINDLE_ENABLE_DDR      DDRH
	#define SPINDLE_ENABLE_PORT     PORTH
	#define SPINDLE_ENABLE_BIT      3 // MEGA2560 Digital Pin 6
	#define SPINDLE_DIRECTION_DDR   DDRE
	#define SPINDLE_DIRECTION_PORT  PORTE
	#define SPINDLE_DIRECTION_BIT   3 // MEGA2560 Digital Pin 5

	// Define flood and mist coolant enable output pins.
	#define COOLANT_FLOOD_DDR   DDRH
	#define COOLANT_FLOOD_PORT  PORTH
	#define COOLANT_FLOOD_BIT   5 // MEGA2560 Digital Pin 8
	#define COOLANT_MIST_DDR    DDRH
	#define COOLANT_MIST_PORT   PORTH
	#define COOLANT_MIST_BIT    6 // MEGA2560 Digital Pin 9

  // Define user-control CONTROLs (cycle start, reset, feed hold) input pins.
  // NOTE: All CONTROLs pins must be on the same port and not on a port with other input pins (limits).
  #define CONTROL_DDR       DDRK
  #define CONTROL_PIN       PINK
  #define CONTROL_PORT      PORTK
  #define CONTROL_RESET_BIT         0  // MEGA2560 Analog Pin 8
  #define CONTROL_FEED_HOLD_BIT     1  // MEGA2560 Analog Pin 9
  #define CONTROL_CYCLE_START_BIT   2  // MEGA2560 Analog Pin 10
  #define CONTROL_SAFETY_DOOR_BIT   3  // MEGA2560 Analog Pin 11
  #define CONTROL_INT       PCIE2  // Pin change interrupt enable pin
  #define CONTROL_INT_vect  PCINT2_vect
  #define CONTROL_PCMSK     PCMSK2 // Pin change interrupt register
  #define CONTROL_MASK      ((1<<CONTROL_RESET_BIT)|(1<<CONTROL_FEED_HOLD_BIT)|(1<<CONTROL_CYCLE_START_BIT)|(1<<CONTROL_SAFETY_DOOR_BIT))

  // Define probe switch input pin.
  #define PROBE_DDR       DDRK
  #define PROBE_PIN       PINK
  #define PROBE_PORT      PORTK
  #define PROBE_BIT       7  // MEGA2560 Analog Pin 15
  #define PROBE_MASK      (1<<PROBE_BIT)

  // Advanced Configuration Below You should not need to touch these variables
  // Set Timer up to use TIMER4B which is attached to Digital Pin 7
  #define SPINDLE_PWM_MAX_VALUE     1024.0 // Translates to about 1.9 kHz PWM frequency at 1/8 prescaler
  #ifndef SPINDLE_PWM_MIN_VALUE
    #define SPINDLE_PWM_MIN_VALUE   1   // Must be greater than zero.
  #endif
  #define SPINDLE_PWM_OFF_VALUE     0
  #define SPINDLE_PWM_RANGE         (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE)
  #define SPINDLE_TCCRA_REGISTER		TCCR4A
  #define SPINDLE_TCCRB_REGISTER		TCCR4B
  #define SPINDLE_OCR_REGISTER	  	OCR4B
  #define SPINDLE_COMB_BIT			    COM4B1

  // 1/8 Prescaler, 16-bit Fast PWM mode
  #define SPINDLE_TCCRA_INIT_MASK ((1<<WGM40) | (1<<WGM41))
  #define SPINDLE_TCCRB_INIT_MASK ((1<<WGM42) | (1<<WGM43) | (1<<CS41))
  #define SPINDLE_OCRA_REGISTER   OCR4A // 16-bit Fast PWM mode requires top reset value stored here.
  #define SPINDLE_OCRA_TOP_VALUE  0x0400 // PWM counter reset value. Should be the same as PWM_MAX_VALUE in hex.

  // Define spindle output pins.
  #define SPINDLE_PWM_DDR	DDRH
  #define SPINDLE_PWM_PORT  PORTH
  #define SPINDLE_PWM_BIT		4 // MEGA2560 Digital Pin 7

#endif

/*
#ifdef CPU_MAP_CUSTOM_PROC
  // For a custom pin map or different processor, copy and edit one of the available cpu
  // map files and modify it to your needs. Make sure the defined name is also changed in
  // the config.h file.
#endif
*/

#endif
